@echo off
REM scripts/build-and-push.bat

setlocal enabledelayedexpansion

REM 配置变量
set REGISTRY_URL=localhost:5000
set IMAGE_NAME=gd-data-platform
set VERSION=%1
if "%VERSION%"=="" set VERSION=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
set FULL_IMAGE_NAME=%REGISTRY_URL%/%IMAGE_NAME%:%VERSION%

echo 开始构建和推送 Docker 镜像...
echo 镜像名称: %FULL_IMAGE_NAME%

REM 检查 Docker 是否运行
docker info >nul 2>&1
if errorlevel 1 (
    echo 错误: Docker 未运行
    exit /b 1
)

REM 登录到私有仓库
echo 登录到私有仓库...
docker login %REGISTRY_URL% -u admin -p admin123

REM 构建应用
echo 构建 Spring Boot 应用...
mvn clean package -DskipTests

REM 构建 Docker 镜像
echo 构建 Docker 镜像...
docker build -t %FULL_IMAGE_NAME% .

REM 推送镜像到私有仓库
echo 推送镜像到私有仓库...
docker push %FULL_IMAGE_NAME%

REM 创建 latest 标签
echo 创建 latest 标签...
docker tag %FULL_IMAGE_NAME% %REGISTRY_URL%/%IMAGE_NAME%:latest
docker push %REGISTRY_URL%/%IMAGE_NAME%:latest

echo 构建和推送完成！
echo 镜像地址: %FULL_IMAGE_NAME%
echo Latest 镜像: %REGISTRY_URL%/%IMAGE_NAME%:latest
